import json
import random
import time

from pip._vendor import requests
from selenium import webdriver
from selenium.webdriver.chrome.options import Options



class ZhiHu:
    def __init__(self):

        self.chrome_options = Options()
        self.chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")  #  前面设置的端口号
        self.browser = webdriver.Chrome(executable_path=r'C:\Program Files\Python38\Scripts\chromedriver.exe', options=self.chrome_options)  # executable执行webdriver驱动的文件

    def get_start(self, brand):
        brandSp = brand.split('/')
        url = 'https://www.zhihu.com/search?type=topic&q=' + brand
        if (len(brandSp) > 1):
            url = 'https://www.zhihu.com/search?type=topic&q=' + brandSp[1]
        print(url)
        self.browser.get(url)
        # time.sleep(20)  # 可以选择手动登录或者是自动化，我这里登录过就直接登陆了
        time.sleep(random.randint(2, 4))
        requests.post("http://localhost/pareHtml?brand=" + brand, json={'htmlStr': self.browser.page_source})
        time.sleep(random.randint(20, 50))

if __name__ == '__main__':
    # chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\selenum\AutomationProfile"
    zhihu = ZhiHu()
    data = requests.get("http://localhost/getData")
    text = data.json()
    print(text)
    index = len(text)
    # zhihu.get_start("陌陌")
    for i in range(0, index):
        # print(text[i])
        zhihu.get_start(text[i])